package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ydd.dto.AdvposDTO;

import com.ydd.entity.Advpos;
import com.ydd.mapper.AdvimgMapper;
import com.ydd.mapper.AdvposMapper;

import com.ydd.service.IAdvposService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.AdvposVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Service
public class AdvposServiceImpl extends ServiceImpl<AdvposMapper, Advpos> implements IAdvposService {

    @Autowired
    private AdvposMapper advposMapper;
    @Autowired
    private AdvimgMapper advimgMapper;
    @Override
    public void upda(Advpos advpos) {
        advposMapper.updateById(advpos);
    }

    @Override
    public void del(Integer id) {

        advposMapper.deleteById(id);

    }

    @Override
    public void upd(Advpos advpos) {

    }

    @Override
    public void add(Advpos advpos) {

        advposMapper.insert(advpos);

    }

    @Override
    public Advpos rowInfo(Integer id) {

        Advpos advpos = advposMapper.selectById(id);
        return advpos;
    }

    @Override
    public List<Advpos> all() {

        List<Advpos> advpos = advposMapper.selectList(null);

        return advpos;
    }


    @Override
    public Map<String, Object> selectPage(AdvposDTO advposDTO) {


        Page<Advpos> page = new Page<>(advposDTO.getPage(),advposDTO.getLimit());
        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(advposDTO.getAdvposdesc()!=null,Advpos::getAdvposdesc,advposDTO.getAdvposdesc());
        queryWrapper.like(advposDTO.getAdvposname()!=null,Advpos::getAdvposname,advposDTO.getAdvposname());
        advposMapper.selectPage(page,queryWrapper);
        List<Advpos> records = page.getRecords();
        List<AdvposVO> advposVOList = new ArrayList<>();
        for (Advpos record : records) {
            Integer id = record.getId();
            String s = id.toString();
            Integer countNum = advimgMapper.countById(s);
            AdvposVO advposVO = new AdvposVO();
            advposVO.setAdvposnum(countNum);
            BeanUtils.copyProperties(record,advposVO);
            advposVOList.add(advposVO);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getTotal());
        map.put("currentPage",page.getCurrent());
        map.put("data",advposVOList);
        map.put("pageSize",advposDTO.getLimit());
        map.put("totalPage",page.getPages());
        return map;
    }

}
